setNavigationScrolling();
setVideoBackgroundImageSize();
sidebarReposition();
setFAQCollapsible();

// Vimeo
var iframe = $('#vimeoplayer')[0];
var player = $f(iframe);

// When the player is ready, add listeners for pause, finish, and playProgress
player.addEvent('ready', function() {
    setVideoPlayButton();
});

$(window).resize(function () {
	sidebarReposition();
});

$(window).scroll(function () {
	sidebarReposition();
});

function scrollToSection(sectionName) {
	$('html, body').animate({
		scrollTop: $(sectionName).offset().top - 40
	}, 500, "swing", function() {
		window.location.hash = sectionName;
	});
}

function setNavigationScrolling() {
	$('#navigation li').find('a').click(function(e) {
		e.preventDefault();
		scrollToSection($(this).attr('href'));
	});
}

function setVideoBackgroundImageSize() {
	var backgroundWidth = 1426;
	var backgroundHeight = 692;
	var ratio = $(window).width() / backgroundWidth;
	if (ratio > 1) {
		$('.video').css('background-size', $(window).width()+'px', (backgroundHeight * ratio)+'px');
	}
	else {
		$('.video').css('background-position', (($(window).width() - backgroundWidth) / 2)+'px', '0');
	}
}

function sidebarReposition() {
	$('#sidebar').css('left', ($('#main').offset().left + 770 - $(window).scrollLeft())+'px');
}

function setFAQCollapsible() {
	$('.faq_box h3').click(function() {
		var faqBox = $(this).parent();
		var paragraph = faqBox.children('p');
		if (!paragraph.data('height')) {
			paragraph.data('height', paragraph.height() + 20);
		}
		var originalHeight = paragraph.data('height');
		
		if (faqBox.hasClass('active')) {
			paragraph.css('opacity', '1');
			paragraph.animate({
				opacity: 0,
				height: 0
			}, 400, function() {
				faqBox.removeClass('active');
			});
		}
		else {
			faqBox.addClass('active');
			paragraph.height(0);
			paragraph.css('opacity', '0');
			paragraph.animate({
				opacity: 1,
				height: originalHeight + 20
			}, 400);
		}
	});
}